package cn.ccew.common.utils;

import java.security.MessageDigest;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;


public class RsetToJson {

	public static final String ToJson(ResultSet rs) throws SQLException {

		ResultSetMetaData metaData = rs.getMetaData();
		JSONArray jsonArray = new JSONArray();

		int colum = metaData.getColumnCount();
		JSONObject jsonElement;
		for (; rs.next(); jsonArray.add(jsonElement)) {
			jsonElement = new JSONObject();
			for (int i = 1; i <= colum; i++) {
				int type = metaData.getColumnType(i);
				String name = metaData.getColumnName(i);
				String namenew = name.toLowerCase();

				if ((93 == type) || (92 == type) || (91 == type))
					jsonElement.put(namenew, rs.getString(i));
				else
					jsonElement.put(namenew, rs.getObject(i));
			}
		}
		return jsonArray.toString();
	}

	public static final String ToJsonForTree(ResultSet rs) throws SQLException {
		ResultSetMetaData metaData = rs.getMetaData();
		JSONArray jsonArray = new JSONArray();

		int colum = metaData.getColumnCount();
		JSONObject jsonElement;
		for (; rs.next(); jsonArray.add(jsonElement)) {
			jsonElement = new JSONObject();
			for (int i = 1; i <= colum; i++) {
				int type = metaData.getColumnType(i);
				String name = metaData.getColumnName(i);
				String namenew = name.toLowerCase();

				if ((93 == type) || (92 == type) || (91 == type))
					jsonElement.put(namenew, rs.getString(i));
				else
					jsonElement.put(namenew, rs.getObject(i));
			}
		}
		return jsonArray.toString().replace("\"false\"", "false")
				.replace("\"true\"", "true");
	}

	public static final String ListToJson(Object objectList)
			throws SQLException {
		JSONArray json = JSONArray.fromObject(objectList);
		return json.toString();
	}

	public static final Object[] JsonToObjectList(String json, Class clazz) {
		JSONArray array = JSONArray.fromObject(json);
		Object[] obj = new Object[array.size()];
		for (int i = 0; i < array.size(); i++) {
			JSONObject jsonObject = array.getJSONObject(i);
			obj[i] = JSONObject.toBean(jsonObject, clazz);
		}
		return obj;
	}

	public static String getMD5Str(String str) throws Exception {

		MessageDigest messageDigest = null;
		messageDigest = MessageDigest.getInstance("MD5");
		messageDigest.reset();
		messageDigest.update(str.getBytes("UTF-8"));
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}

	public static String getIpAddr(HttpServletRequest request) {

		String ip = request.getHeader("X-Real-IP");
		if ((!StringUtils.isBlank(ip)) && (!"unknown".equalsIgnoreCase(ip))) {
			return ip;
		}
		ip = request.getHeader("X-Forwarded-For");
		if ((!StringUtils.isBlank(ip)) && (!"unknown".equalsIgnoreCase(ip))) {

			int index = ip.indexOf(',');
			if (index != -1) {
				return ip.substring(0, index);
			}
			return ip;
		}
		return request.getRemoteAddr();
	}
}
